package com.example.xymarket.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xymarket.adapter.MessageListAdapter;
import com.example.xymarket.common.ChatConfig;
import com.example.xymarket.myView.StatusBar;
import com.example.xymarket.R;
import com.example.xymarket.cim.http.SendMessageManager;
import com.example.xymarket.httpUtil.MessageService;
import com.example.xymarket.common.MyHandler;
import com.farsunset.cim.sdk.android.CIMEventListener;
import com.farsunset.cim.sdk.android.CIMListenerManager;
import com.farsunset.cim.sdk.android.CIMPushManager;
import com.farsunset.cim.sdk.android.model.Message;
import com.farsunset.cim.sdk.android.model.ReplyBody;
import com.farsunset.cim.sdk.android.model.SentBody;

import java.util.List;

public class ChatActivity extends AppCompatActivity implements CIMEventListener {
    private MessageListAdapter adapter;
    private RecyclerView messageListView;
    private EditText Mess_input;
    private SendMessageManager sendMessageManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);

        CIMListenerManager.registerMessageListener(this);
        messageListView = findViewById(R.id.recyclerView);
        messageListView.setLayoutManager(new LinearLayoutManager(this));
        messageListView.setItemAnimator(new DefaultItemAnimator());
        TextView textView=findViewById(R.id.chat_name);
        textView.setText(ChatConfig.him.name);
        StatusBar.setStatusBarColor(this,StatusBar.yellow);
        Mess_input=findViewById(R.id.mess_input);
        sendMessageManager=new SendMessageManager(handler2);
        MyHandler handler=new MyHandler(this,handler2);
        MessageService messageService=new MessageService(handler);
        messageService.getChatRecord(ChatConfig.him.id);
    }


    @SuppressLint("HandlerLeak")
    final Handler handler2 = new Handler()
    {
        public void handleMessage(android.os.Message msg) {
            super.handleMessage(msg);
            if(msg.what==1) {
                com.farsunset.cim.sdk.android.model.Message message=(com.farsunset.cim.sdk.android.model.Message) msg.obj;
                adapter.add(message);
                messageListView.smoothScrollToPosition(adapter.getItemCount()-1);
            }

            if(msg.what==100) {
                List<com.farsunset.cim.sdk.android.model.Message> message=(List<com.farsunset.cim.sdk.android.model.Message>) msg.obj;
                adapter = new MessageListAdapter(message);
                messageListView.setAdapter(adapter);
                messageListView.smoothScrollToPosition(adapter.getItemCount());
            }
        }
    };

    public void SendMessage(View view)
    {
        if(Mess_input.getText().toString().equals(""))
        {
            Toast.makeText(this,"请输入消息",Toast.LENGTH_LONG).show();
            return;
        }
        Message message=new Message();
        message.setAction("0");
        message.setSender(ChatConfig.me.id);
        message.setReceiver(ChatConfig.him.id);
        message.setContent(Mess_input.getText().toString());
        Mess_input.setText("");
        sendMessageManager.send(message);
    }

    @Override
    public void onMessageReceived(Message message) {
        try {
            adapter.add(message);
            messageListView.smoothScrollToPosition(adapter.getItemCount()-1);
        }
        catch (Exception e)
        {
            System.out.println(e.toString());
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();

        /*
         * 页面关闭记得调用此方法喔
         */
        CIMListenerManager.removeMessageListener(this);

        /*
         * 退出应用记得调用此方法喔
         */
        CIMPushManager.destroy(this);
    }

    /**
     * 当调用CIMPushManager.sendRequest()向服务端发送请求，获得相应时调用
     *
     * @param replyBody
     */
    @Override
    public void onReplyReceived(ReplyBody replyBody) {

    }

    /**
     * 当调用CIMPushManager.sendRequest()向服务端发送请求成功时
     *
     * @param sentBody
     */
    @Override
    public void onSendFinished(SentBody sentBody) {


    }

    @Override
    public void onNetworkChanged(NetworkInfo networkInfo) {

    }

    @Override
    public void onConnectFinished(boolean b) {

    }

    @Override
    public void onConnectionClosed() {

    }

    @Override
    public void onConnectFailed() {

    }

    @Override
    public int getEventDispatchOrder() {
        return 0;
    }
}